package org.syh.boyangfighthealthclub.controller;

import org.syh.boyangfighthealthclub.entity.User;
import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Ben
 * @Date: 2021/3/9 9:37
 */
@WebServlet("/admin/insertUser")
public class InsertUser extends HttpServlet {
    @Override
    protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;utf-8");
        String nickname=servletRequest.getParameter("nickName");
        String userName=servletRequest.getParameter("userName");
        String password=servletRequest.getParameter("password");
        String email=servletRequest.getParameter("email");
        String sex=servletRequest.getParameter("sex");
        Integer age=null;
        boolean test;
        try{
            age= Integer.valueOf(servletRequest.getParameter("age"));
            test=true;
        }catch (Exception e){
            e.printStackTrace();
            test=false;
        }
        if ("".equals(nickname) || "".equals(userName) || "".equals(password) || "".equals(email) || "".equals(sex)||!test) {
            servletResponse.sendRedirect("../fail.jsp");
        }
        IUserService iUserService=new UserServiceImpl();
        boolean judgment=iUserService.insertUser(new User(nickname,userName,password,email,sex,age,"用户"));
        if (judgment){
            servletResponse.sendRedirect("../success.jsp");
        }
    }
}
